package com.example.simpledemo1.aggregate.sortDemo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: ZhangLiang
 * @Date: 2021/06/29/13:33
 * @Description: Comparator定制排序
 */
public class ComparatorDemo {

    public static void main(String[] args) {
        ArrayList<Integer> arrayList=new ArrayList<>();
        arrayList.add(-1);
        arrayList.add(2);
        arrayList.add(3);
        arrayList.add(1);
        arrayList.add(9);
        arrayList.add(6);
        arrayList.add(44);
        arrayList.add(33);
        arrayList.add(-9);
        System.out.println("原始数组:");
        System.out.println(arrayList);
        // void reverse(List list)：反转倒序
        Collections.reverse(arrayList);
        System.out.println("Collections.reverse(List list):反转倒序");
        System.out.println(arrayList);

        // void sort(List list),按自然排序的升序排序
        Collections.sort(arrayList);
        System.out.println("Collections.sort(arrayList):自然排序升序");
        System.out.println(arrayList);

        Collections.sort(arrayList, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                //自定义倒序
                return o2.compareTo(o1);
            }
        });
        System.out.println("定制排序后");
        System.out.println(arrayList);


        ComparatorDemo demo=new ComparatorDemo();
        System.out.println(demo.toString());
    }

    @Override
    public String toString() {
        return getClass().getName()+"@"+Integer.toHexString(hashCode());
    }

}
